import json

from com.maycur.gateway.common.base_entity import ApiTaskSet
from locust import task


class EmployeeTask(ApiTaskSet):

    employee_user_code: str

    @task(1)
    def list_employee(self):
        list_employee_url = f'{self.get_app_host()}/api/web/config/employee/employees'
        request_body = {
            "keyword": "",
            "employeeStatus": ["ENABLE"],
            "length": 20,
            "start": 0
        }
        json_body = json.dumps(request_body)
        headers = self.get_headers_with_content_type()
        with self.client.post(list_employee_url, data=json_body, headers=headers, catch_response=True) as res:
            result_dto = self.parse_response(res)
            if len(result_dto.data['data']) > 0:
                first_employee = result_dto.data['data'][0]
                self.employee_user_code = first_employee['userCode']

    @task(1)
    def employee_detail(self):
        if not hasattr(self, 'employee_user_code'):
            return
        employee_detail_url = f'{self.get_app_host()}/api/web/config/employee/{self.employee_user_code}'
        self.client.get(employee_detail_url, headers=self.get_headers())

